package com.fr.data.impl;

import com.fr.base.DefaultValues;
import com.fr.base.FRContext;
import com.fr.base.XMLable;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.Mapper;
import com.fr.base.xml.BaseXMLUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.AbstractTableData;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.report.io.xml.SynchronizedVersion;
import com.fr.report.script.Primitive;
import com.fr.util.Utils;
import java.awt.Image;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EmbeddedTableData extends AbstractTableData
  implements XMLable
{
  private List columnNameList = new ArrayList();
  private List columnClassList = new ArrayList();
  private List rowData = new ArrayList();
  private static final Mapper CLASS2NAME = new Mapper()
  {
    public Object map(int paramInt, Object paramObject, List paramList)
    {
      return ((paramObject instanceof Class) ? ((Class)paramObject).getName() : null);
    }
  };

  public void addColumn(String paramString, Class paramClass)
  {
    this.columnNameList.add(paramString);
    this.columnClassList.add(paramClass);
    for (int i = 0; i < this.rowData.size(); ++i)
    {
      List localList = (List)this.rowData.get(i);
      localList.add(getDefaultValueOfClass(paramClass));
    }
  }

  public void insertColumn(String paramString, Class paramClass, int paramInt)
  {
    this.columnNameList.add(paramInt + 1, paramString);
    this.columnClassList.add(paramInt + 1, paramClass);
    for (int i = 0; i < this.rowData.size(); ++i)
    {
      List localList = (List)this.rowData.get(i);
      localList.add(paramInt + 1, getDefaultValueOfClass(paramClass));
    }
  }

  public void setColumn(int paramInt, String paramString, Class paramClass)
  {
    if ((paramInt < 0) || (paramInt >= this.columnNameList.size()))
      return;
    this.columnNameList.set(paramInt, paramString);
    this.columnClassList.set(paramInt, paramClass);
    for (int i = 0; i < this.rowData.size(); ++i)
    {
      List localList = (List)this.rowData.get(i);
      Object localObject = localList.get(paramInt);
      if (localObject == null)
        break label109:
      localList.set(paramInt, getValueOfClass(localObject, paramClass));
    }
    label109:
  }

  public void removeColumn(int paramInt)
  {
    if ((paramInt < 0) || (paramInt >= this.columnNameList.size()))
      return;
    this.columnNameList.remove(paramInt);
    this.columnClassList.remove(paramInt);
    int i = this.rowData.size();
    for (int j = 0; j < i; ++j)
    {
      List localList = (List)this.rowData.get(j);
      localList.remove(paramInt);
    }
  }

  public void addRow(List paramList)
  {
    addRow(getRowCount(), paramList);
  }

  public void addRow(int paramInt, List paramList)
  {
    int j;
    int i = getColumnCount();
    if (paramList.size() < i)
      for (j = paramList.size(); j < i; ++j)
        paramList.add(getDefaultValueOfClass(getColumnClass(j)));
    if (paramInt >= this.rowData.size())
      this.rowData.add(paramList);
    else
      this.rowData.add(paramInt, paramList);
  }

  public void addNewRow(int paramInt)
  {
    int i = getColumnCount();
    if (i <= 0)
      return;
    ArrayList localArrayList = new ArrayList(i);
    for (int j = 0; j < i; ++j)
      localArrayList.add(getDefaultValueOfClass(getColumnClass(j)));
    addRow(paramInt, localArrayList);
  }

  public void removeRow(int paramInt)
  {
    if ((paramInt < 0) || (paramInt >= this.rowData.size()))
      return;
    this.rowData.remove(paramInt);
  }

  private Object getDefaultValueOfClass(Class paramClass)
  {
    if (paramClass == Integer.class)
      return new Integer(0);
    if ((paramClass == Double.class) || (paramClass == Float.class))
      return new Double(0.0D);
    if (paramClass == Date.class)
      return new Date();
    if (paramClass == Boolean.class)
      return Boolean.TRUE;
    if (paramClass == Image.class)
      return "Image";
    return "";
  }

  public int getColumnCount()
  {
    return this.columnNameList.size();
  }

  public String getColumnName(int paramInt)
  {
    String str = (String)this.columnNameList.get(paramInt);
    return str;
  }

  public Class getColumnClass(int paramInt)
  {
    if ((paramInt >= 0) && (paramInt < this.columnClassList.size()))
      return ((Class)this.columnClassList.get(paramInt));
    return String.class;
  }

  public int getRowCount()
  {
    return this.rowData.size();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
  {
    List localList = (List)this.rowData.get(paramInt1);
    if ((paramInt2 < 0) || (paramInt2 >= localList.size()))
      return null;
    Object localObject = localList.get(paramInt2);
    if (localObject == null)
      return Primitive.NULL;
    return localObject;
  }

  private Object getValueOfClass(Object paramObject, Class paramClass)
  {
    if (paramObject == null)
      return Primitive.NULL;
    if (paramObject.getClass() == paramClass)
      return paramObject;
    String str = paramObject.toString();
    try
    {
      if (paramClass == Integer.class)
      {
        Float localFloat = Float.valueOf(str);
        return new Integer(Math.round(localFloat.floatValue()));
      }
      if ((paramClass == Double.class) || (paramClass == Float.class))
        return new Double(str);
      if (paramClass == Date.class)
        return new Date(Long.parseLong(str));
      if (paramClass == Boolean.class)
        return Boolean.valueOf(str);
      if (paramClass == Image.class)
        return "Image";
      return str;
    }
    catch (Exception localException)
    {
    }
    return getDefaultValueOfClass(paramClass);
  }

  public void setValueAt(Object paramObject, int paramInt1, int paramInt2)
  {
    if ((paramInt2 < 0) || (paramInt2 >= getColumnCount()) || (paramInt1 < 0) || (paramInt1 >= getRowCount()))
      return;
    Object localObject = (List)this.rowData.get(paramInt1);
    if (localObject == null)
      localObject = new ArrayList();
    if ((((List)localObject).size() <= paramInt2) || (((List)localObject).get(paramInt2) == null))
    {
      for (int i = ((List)localObject).size(); i < paramInt2; ++i)
        ((List)localObject).add(i, "");
      ((List)localObject).add(paramInt2, paramObject);
    }
    else
    {
      ((List)localObject).set(paramInt2, paramObject);
    }
  }

  public String toString()
  {
    return "EmbeddedTableData[ColumnCount:" + getColumnCount() + ",RowCount" + getRowCount() + "]";
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    EmbeddedTableData localEmbeddedTableData = (EmbeddedTableData)super.clone();
    ArrayList localArrayList1 = new ArrayList();
    localEmbeddedTableData.columnNameList = localArrayList1;
    for (int i = 0; i < this.columnNameList.size(); ++i)
      localArrayList1.add(this.columnNameList.get(i));
    ArrayList localArrayList2 = new ArrayList();
    localEmbeddedTableData.columnClassList = localArrayList2;
    for (int j = 0; j < this.columnClassList.size(); ++j)
      localArrayList2.add(this.columnClassList.get(j));
    ArrayList localArrayList3 = new ArrayList();
    localEmbeddedTableData.rowData = localArrayList3;
    for (int k = 0; k < this.rowData.size(); ++k)
    {
      int l;
      ArrayList localArrayList4 = new ArrayList();
      localArrayList3.add(localArrayList4);
      List localList = (List)this.rowData.get(k);
      if (localList != null)
        for (l = 0; l < localList.size(); ++l)
          localArrayList4.add(localList.get(l));
    }
    return localEmbeddedTableData;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    String[] arrayOfString1;
    int j;
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isAttr())
      this.columnClassList = new ArrayList();
    if (paramXMLableReader.isChildNode())
    {
      String str1;
      Object localObject;
      String str2 = paramXMLableReader.getTagName();
      if (str2.equals("XMLVersion"))
      {
        if ((str1 = paramXMLableReader.getElementValue()) != null)
        {
          localObject = SynchronizedVersion.getSynchronizedVersion(Thread.currentThread());
          ((SynchronizedVersion)localObject).setXmlVersionByString(str1);
        }
      }
      else
      {
        int i;
        if ("ColumnNames".equals(str2))
        {
          if ((str1 = paramXMLableReader.getElementValue()) == null)
            return;
          this.columnNameList.clear();
          localObject = Utils.splitString(str1, ",,.,,");
          i = 0;
          while (true)
          {
            if (i >= localObject.length)
              return;
            this.columnNameList.add(localObject[i]);
            ++i;
          }
        }
        if ("ColumnTypes".equals(str2))
        {
          if ((str1 = paramXMLableReader.getElementValue()) == null)
            return;
          localObject = Utils.splitString(str1, ",");
          i = 0;
          while (true)
          {
            if (i >= localObject.length)
              return;
            try
            {
              this.columnClassList.add(FRCoreContext.classForName(localObject[i]));
            }
            catch (Exception localException)
            {
              this.columnClassList.add(String.class);
            }
            ++i;
          }
        }
        if ("RowData".equals(str2))
        {
          localObject = null;
          if (SynchronizedVersion.isAfterEM_CODE_XML_VERSION())
            try
            {
              localObject = new String(BaseXMLUtils.readByteArray(paramXMLableReader), "utf-8");
            }
            catch (UnsupportedEncodingException localUnsupportedEncodingException)
            {
              FRContext.getLogger().log(Level.WARNING, localUnsupportedEncodingException.getMessage(), localUnsupportedEncodingException);
            }
          else
            localObject = new String(BaseXMLUtils.readByteArray(paramXMLableReader));
          if ((this.columnClassList.size() <= 0) || (localObject == null))
            return;
          this.rowData.clear();
          arrayOfString1 = Utils.splitString((String)localObject, '\n');
          for (j = 0; j < arrayOfString1.length; ++j)
          {
            String[] arrayOfString2 = Utils.splitString(arrayOfString1[j], '\t');
            ArrayList localArrayList = new ArrayList();
            this.rowData.add(localArrayList);
            for (int k = 0; k < arrayOfString2.length; ++k)
            {
              String str3 = Utils.replaceAllString(arrayOfString2[k], new String[] { "&t;", "&n;" }, new String[] { "\t", "\n" });
              Class localClass = (Class)this.columnClassList.get(k);
              if (ComparatorUtils.equals(Double.class, localClass))
              {
                try
                {
                  localArrayList.add(Double.valueOf(str3));
                }
                catch (NumberFormatException localNumberFormatException1)
                {
                  localArrayList.add(new Double(0.0D));
                }
              }
              else if (ComparatorUtils.equals(Integer.class, localClass))
              {
                try
                {
                  localArrayList.add(Integer.valueOf(str3));
                }
                catch (NumberFormatException localNumberFormatException2)
                {
                  localArrayList.add(new Integer(0));
                }
              }
              else if (ComparatorUtils.equals(Boolean.class, localClass))
              {
                localArrayList.add(Boolean.valueOf(str3));
              }
              else if (ComparatorUtils.equals(Image.class, localClass))
              {
                String[] arrayOfString3 = Utils.splitString(str3, ",;,");
                if ((arrayOfString3 == null) || (arrayOfString3.length != 3))
                  FRContext.getLogger().log(Level.WARNING, "Error to load embbed image object");
                else
                  localArrayList.add(BaseXMLUtils.decodeImage(Integer.parseInt(arrayOfString3[0]), Integer.parseInt(arrayOfString3[1]), arrayOfString3[2].getBytes()));
              }
              else
              {
                localArrayList.add(str3);
              }
            }
          }
        }
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("ColumnNames").textNode(BaseCoreUtils.join(this.columnNameList, ",,.,,")).end();
    paramXMLPrintWriter.startTAG("ColumnTypes").textNode(BaseCoreUtils.join(BaseCoreUtils.map(this.columnClassList, CLASS2NAME), ",")).end();
    if (this.rowData.size() <= 0)
      return;
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.rowData.size(); ++i)
    {
      if (i != 0)
        localStringBuffer.append('\n');
      List localList = (List)this.rowData.get(i);
      for (int j = 0; j < localList.size(); ++j)
      {
        if (j != 0)
          localStringBuffer.append('\t');
        Object localObject1 = localList.get(j);
        if (localObject1 == null)
        {
          localStringBuffer.append("");
        }
        else
        {
          Class localClass = (Class)this.columnClassList.get(j);
          if ((localClass == Integer.class) || (localClass == Double.class) || (localClass == Float.class) || (localClass == Boolean.class))
          {
            localStringBuffer.append(Utils.objectToString(localObject1));
          }
          else
          {
            Object localObject2;
            if ((localClass == Date.class) && (localObject1 instanceof Date))
            {
              localObject2 = FRContext.getDefaultValues();
              localStringBuffer.append(((DefaultValues)localObject2).getDateTimeFormat().format(localObject1));
            }
            else if (localClass == Image.class)
            {
              localObject2 = (Image)localObject1;
              GraphHelper.waitForImage((Image)localObject2);
              int k = ((Image)localObject2).getWidth(null);
              int l = ((Image)localObject2).getHeight(null);
              localStringBuffer.append(k);
              localStringBuffer.append(",;,");
              localStringBuffer.append(l);
              localStringBuffer.append(",;,");
              byte[] arrayOfByte = BaseXMLUtils.encodeImage((Image)localObject2);
              localStringBuffer.append(arrayOfByte);
            }
            else
            {
              localObject2 = Utils.objectToString(localObject1);
              localObject2 = BaseCoreUtils.encodeString((String)localObject2, new String[][] { { "&t;", "&n;" }, { "\t", "\n" } });
              localStringBuffer.append((String)localObject2);
            }
          }
        }
      }
    }
    paramXMLPrintWriter.startTAG("RowData");
    try
    {
      BaseXMLUtils.writeByteArray(paramXMLPrintWriter, localStringBuffer.toString().getBytes("utf-8"));
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      FRContext.getLogger().log(Level.WARNING, localUnsupportedEncodingException.getMessage(), localUnsupportedEncodingException);
    }
    paramXMLPrintWriter.end();
  }

  public static EmbeddedTableData embedify(TableData paramTableData)
    throws TableDataException
  {
    return embedify(paramTableData, -1);
  }

  public void clear()
  {
    this.columnClassList.clear();
    this.columnNameList.clear();
    this.rowData.clear();
  }

  public static EmbeddedTableData embedify(TableData paramTableData, int paramInt)
    throws TableDataException
  {
    EmbeddedTableData localEmbeddedTableData = new EmbeddedTableData();
    if (paramTableData != null)
    {
      int i = paramTableData.getColumnCount();
      Class[] arrayOfClass = new Class[i];
      String[] arrayOfString = new String[i];
      for (int j = 0; j < i; ++j)
        arrayOfString[j] = paramTableData.getColumnName(j);
      if (paramInt < 0)
        paramInt = paramTableData.getRowCount();
      else if ((paramInt != 0) && (!(paramTableData.hasRow(paramInt - 1))))
        paramInt = paramTableData.getRowCount();
      for (j = 0; j < paramInt; ++j)
      {
        ArrayList localArrayList = new ArrayList();
        for (int k = 0; k < i; ++k)
        {
          Object localObject = paramTableData.getValueAt(j, k);
          if ((arrayOfClass[k] == null) && (localObject != null))
            arrayOfClass[k] = localObject.getClass();
          localArrayList.add(localObject);
        }
        localEmbeddedTableData.addRow(localArrayList);
      }
      localEmbeddedTableData.columnClassList = new ArrayList();
      localEmbeddedTableData.columnClassList.addAll(Arrays.asList(arrayOfClass));
      localEmbeddedTableData.columnNameList = new ArrayList();
      localEmbeddedTableData.columnNameList.addAll(Arrays.asList(arrayOfString));
      try
      {
        paramTableData.release();
      }
      catch (Exception localException)
      {
        throw new TableDataException(localException.getMessage(), localException);
      }
    }
    return localEmbeddedTableData;
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof EmbeddedTableData) && (ComparatorUtils.equals(this.columnClassList, ((EmbeddedTableData)paramObject).columnClassList)) && (ComparatorUtils.equals(this.columnNameList, ((EmbeddedTableData)paramObject).columnNameList)) && (ComparatorUtils.equals(this.rowData, ((EmbeddedTableData)paramObject).rowData)));
  }
}